package
{
	import flash.filters.GlowFilter;
	import flash.net.SharedObject;
	import flash.utils.Dictionary;
	
	import org.flixel.FlxG;
	import org.flixel.FlxGroup;
	import org.flixel.FlxSprite;
	import org.flixel.FlxText;

	public class ModalOpenPopup extends FlxGroup
	{
		protected var Background:FlxSprite;
		protected var localInformation:SharedObject;
		protected var curMapNames:Array;
		protected var spec:PuzzleSpecification;
		protected var done:Boolean;
		protected var cancelButton:FlxSprite;
		protected var openButton:FlxSprite;
		protected var deleteButton:FlxSprite;
		protected var prevButton:FlxSprite;
		protected var nextButton:FlxSprite;
		protected var pageNum:FlxText;
		protected var pageBG:FlxSprite;
		protected var selectFilter:GlowFilter;
		
		protected var pages:Array;
		protected var bgs:Array;
		protected var curPage:int;
		protected var totalPages:int;
		
		protected var bgToName:Dictionary;
		
		protected var container:FlxSprite;
		
		protected var header:FlxSprite;
		
		protected var currentBG:FlxSprite;
		protected var selectedName:String;
		protected var open:Boolean;
		
		public function ModalOpenPopup()
		{
			done = false
			localInformation = SharedObject.getLocal("elementalGameInfo");
			
			var customMaps:String = localInformation.data["customMaps"];
			
			
			if (customMaps != null) {
				curMapNames = customMaps.split(",");
			} else {
				curMapNames = new Array();
			}
			
			//curMapNames = new Array("1abcd2abcd3abcd4abcd","2","3","4","5","6","7","8","9","0","1");
			
			Background = new FlxSprite(83,113,Res.popupBackground);
			
			this.add(Background);
			
			currentBG = new FlxSprite();
			selectFilter = new GlowFilter(0xFFFFAA/*0xe5cda4*/, 0.8, 10, 10, 3,1,true);
			
			container = new FlxSprite();
			container.makeGraphic(250, 220);
			container.drawRect(0, 0, 250, 220, 0x3d300a);
			container.x = 133;
			container.y = 175;
			this.add(container);
			
			header = new FlxSprite(133,150, Res.openHeader);
			this.add(header);
			
			
			deleteButton = new FlxSprite(115, 420, Res.deleteButton);
			openButton = new FlxSprite(220, 420, Res.openMapButton);
			cancelButton = new FlxSprite(325, 420, Res.cancelButton);
			
			pageBG = new FlxSprite(183, 150, Res.pageBG);
			prevButton = new FlxSprite(133, 150, Res.prevButton);
			nextButton = new FlxSprite(333, 150, Res.nextButton);
			
			this.add(deleteButton);
			this.add(openButton);
			this.add(cancelButton);
			
			createPages();
			
			var titleText:FlxText = new FlxText(107, 120, 300, "Open/Delete Puzzle", false);
			titleText.setFormat("Arial", 20, 0x111111, "center");
			this.add(titleText);
		}
		
		private function createPages():void {
			var rows:int = 5;
			var numMaps:int = curMapNames.length;
			var numPages:int = numMaps / 5 + Math.min(1, numMaps % 5);
		
			pages = new Array();
			bgs = new Array();
			bgToName = new Dictionary();
			var tmpArray:Array = curMapNames;
			curMapNames = new Array();
			for (var k:int = 0; k < tmpArray.length; k++) {
				if (tmpArray[k] != "" && tmpArray[k] != null) {
					curMapNames.push(tmpArray[k]);
				}
			}
			for (var i:int = 0; i < numPages; i++) {
				var page:FlxGroup = new FlxGroup;
				var block:FlxGroup = new FlxGroup;
				for (var j:int = 0; i*rows + j < numMaps && j < rows; j++) {
					var n:String = new String(curMapNames[i*rows + j]);
					var nameArray:Array = n.split("_");
					var text:FlxText = new FlxText(140, 195 + j*40, 242, nameArray[1],false);
					var bg:FlxSprite = new FlxSprite(133, 188 + j*40, Res.mapSelectBounds);
					block.add(bg);
					
					//for selection
					bgToName[bg] = curMapNames[i*rows + j];
					text.setFormat("Arial", 20, 0xe5cda4, "left");
					page.add(text);
				}
				bgs.push(block);
				pages.push(page);
			}
			
			if (numPages > 1) {
				this.add(pageBG);
				pageNum = new FlxText(263, 150, 30, "1", false);
				pageNum.setFormat("Arial", 18, 0xFFFFFF,"center");
				this.add(pageNum);
				this.add(prevButton);
				this.add(nextButton);
				curPage = 0;
				totalPages = numPages;
			}
			this.add(bgs[0]);
			this.add(pages[0]);
		}
		
		public override function update():void  {
			super.update();
			
			if (FlxG.mouse.justPressed()) {
				checkOpen();
				checkDelete();
				checkCancel();
				checkPageChange();
				checkMapSelect();
			}
		}
		
		private function checkMapSelect():void {
			if (bgs != null) {
				var currentOptions:Array = (bgs[curPage] as FlxGroup).members;
				for (var i:int = 0; i < currentOptions.length; i++) {
					var op:FlxSprite = (currentOptions[i] as FlxSprite);
					if (op != null && op.overlapsPoint(FlxG.mouse.screenX,FlxG.mouse.screenY)) {
						currentBG.removeFilter();
						currentBG = op;
						currentBG.setFilter(selectFilter);
						selectedName = bgToName[op];
					}
				}
			}
		}
		
		private function checkPageChange():void {
			if (prevButton.overlapsPoint(FlxG.mouse.x, FlxG.mouse.y)) {
				if (curPage != 0) {
					this.remove(pages[curPage], true);
					this.remove(bgs[curPage], true);
					curPage--;
					this.add(bgs[curPage]);
					this.add(pages[curPage]);
					pageNum.text = new String(curPage + 1);
					selectedName = null;
				}
			} else if (nextButton.overlapsPoint(FlxG.mouse.x, FlxG.mouse.y)) {
				if (curPage + 1 < totalPages) {
					this.remove(pages[curPage], true);
					this.remove(bgs[curPage], true);
					curPage++;
					this.add(bgs[curPage]);
					this.add(pages[curPage]);
					pageNum.text = new String(curPage + 1);
					selectedName = null;
				}
			}
		}
		
		private function checkOpen():void {
			if (selectedName != null && selectedName != "" && openButton.overlapsPoint(FlxG.mouse.x, FlxG.mouse.y)) {
				spec = new PuzzleSpecification(localInformation.data[selectedName + "_map"],
											localInformation.data[selectedName + "_tst"],
											localInformation.data[selectedName + "_arw"].split(","),
											localInformation.data[selectedName + "_chi"].split(","),
											int(localInformation.data[selectedName + "_uid"]));
				done = true;
				open = true;
			}
		}
		
		private function checkDelete():void {
			if (selectedName != null && selectedName != "" && deleteButton.overlapsPoint(FlxG.mouse.x, FlxG.mouse.y)) {
				if (curMapNames.length != 0) {
					var res:String = "";
					var i:int = 1;
					if (curMapNames.length > 1 && curMapNames[0] == selectedName) {
						res += curMapNames[1];
						i++;
					} else if (curMapNames.length == 1 && curMapNames[0] == selectedName) {
						res = "";
					} else {
						res += curMapNames[0];
					}
					
					for (var j:int = i; j < curMapNames.length; j++) {
						if (curMapNames[j] != selectedName) {
							res += "," + curMapNames[j];
						}
					}
					localInformation.data["customMaps"] = res;
					localInformation.flush();
					curMapNames = res.split(",");
					this.remove(pages[curPage]);
					this.remove(bgs[curPage]);
					this.remove(pageBG);
					this.remove(pageNum);
					this.remove(prevButton);
					this.remove(nextButton);
					createPages();
				}
			}
		}
		
		private function checkCancel():void {
			if (cancelButton.overlapsPoint(FlxG.mouse.x, FlxG.mouse.y)) {
				done = true;
			}
		}
		
		public function finishedPopup():Boolean {
			return done;
		}
		
		public function wasOpened():Boolean {
			return open;
		}
		
		public function getPuzzleSpec():PuzzleSpecification {
			return spec;
		}
	}
}